package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import config.ColorConfig;
import util.FileUtil;

@SuppressWarnings("serial")
public class FilePanel extends JPanel{

	private JList<String> jlist;
	private JScrollPane jscroll;
	private String[] fileData;
	private JButton jbutton;
	private int width = 200;
	private int height = 0;
	JTextArea jta = new JTextArea();
	
	FilePanel(){
		super();
		fileData = FileUtil.getFiles();
		
		//this
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(width,height));
	}
	
	
	public void start() {	
		
		//jpanel
		this.setBorder(BorderFactory.createLineBorder(ColorConfig.getColor("filePanelColor")));
		this.setFocusable(true);	
	
		//jlist
		jlist = new JList<String>(fileData);
		jlist.addListSelectionListener(e->{
			if(!e.getValueIsAdjusting()) {
				nextFile();
			}
		});
		
		//jbutton
		if(FileUtil.getFileName()!=null) {
			jbutton = new JButton(FileUtil.getFileName());
			jbutton.addActionListener(e->{
				preFile();
			});
			this.add(jbutton,BorderLayout.NORTH);
		}
		
		//jscroll
		jscroll = new JScrollPane(jlist);
		
		this.add(jscroll,BorderLayout.CENTER);
		this.add(jta,BorderLayout.SOUTH);
		
		updateUI();
	}
	
	private void preFile() {
		// TODO Auto-generated method stub
		if(FileUtil.getParentName()!=null) {
			FileUtil.preFile();
			fileData = FileUtil.getFiles();
			removeAll();
			start();
		}
	}


	private void nextFile() {
		FileUtil.nextFile(jlist.getSelectedValue());
		if(!FileUtil.isDirectory()) {
			//do something here
			GUIManager.addPanel(FileUtil.getFileName());
			FileUtil.preFile();
			
		}else {
			fileData = FileUtil.getFiles();
			removeAll();
			start();
		}
	}
	
	public void openDirectory(String fileName) {
		FileUtil.openDirectory(fileName);
		fileData = FileUtil.getFiles();
		removeAll();
		start();
	}
	
}
